package com.exchange.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.exchange.entity.pojo.*;
import com.exchange.entity.vo.RecodVo;
import com.exchange.mapper.DetailPowerMapper;
import com.exchange.mapper.ExchangeRecordMapper;
import com.exchange.mapper.UserDetailMapper;
import com.exchange.service.ElectricCardService;
import com.exchange.mapper.ElectricCardMapper;
import com.exchange.service.ReservationRecordService;
import com.exchange.until.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
* @author Administrator
* @description 针对表【electric_card】的数据库操作Service实现
* @createDate 2024-04-27 19:54:01
*/
@Service
public class ElectricCardServiceImpl extends ServiceImpl<ElectricCardMapper, ElectricCard>
    implements ElectricCardService{
    @Autowired
    private ElectricCardMapper electricCardMapper;

    @Autowired
    private UserDetailMapper userDetailMapper;

    @Autowired
    private DetailPowerMapper detailPowerMapper;

    @Autowired
    private ExchangeRecordMapper exchangeRecordMapper;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private ReservationRecordService reservationRecordService;
    /**
     * 1、将用户当前的电池放回电柜
     * 2、修改用户当前电池电量以及电池编号
     * 3、付钱
     * 4、修改预约表
     * @param token
     * @return
     */
    @Override
    public Result<String> electrics(String token) {
        String s = redisTemplate.opsForValue().get("user:login" + token);
        User user = JSON.parseObject(s, User.class);
        //根据用户获取当前需要更换的电池
        QueryWrapper queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("uid",user.getId());
        UserDetail userDetail = userDetailMapper.selectOne(queryWrapper);

        //查出来旧电池的主键
        Long did1 = userDetail.getDid();
        //查询出当前用户使用的电池---》旧电池
        DetailPower detailPower = detailPowerMapper.selectById(did1);
        detailPower.setStatus(0);
        int i1 = detailPowerMapper.updateById(detailPower);

        //新电池需要在登记表中拿出来
        QueryWrapper<ReservationRecord> reservationRecordQueryWrapper = new QueryWrapper<>();
        //查询当前用户当前预约的电瓶
        reservationRecordQueryWrapper.eq("uid",user.getId());
        reservationRecordQueryWrapper.eq("status",2);
        ReservationRecord reservationRecord = reservationRecordService.getOne(reservationRecordQueryWrapper);
        if (ObjectUtil.isEmpty(reservationRecord)){
            return Result.error("换电失败，请重新预约");
        }

        //查出新电池
        DetailPower detailPower1 = this.detailPowerMapper.selectById(reservationRecord.getDpid());
        detailPower1.setStatus(2);
        int i2 = detailPowerMapper.updateById(detailPower1);

        //当前电池的电量
        Integer currentPower = userDetail.getCurrentPower();
            //需要换的电量
            Integer i=100-currentPower;
        //用户拥有的电卡
        ElectricCard electricCard = electricCardMapper.selectOne(queryWrapper);
        BigDecimal balance = electricCard.getBalance();
        electricCard.setBalance(balance.subtract(new BigDecimal(i)));
        //修改用户当前绑定的电瓶
        userDetail.setDid(detailPower1.getId());
        userDetail.setCurrentPower(100);
        userDetail.setCurrentMileage(0);
        int updateUserDetail = userDetailMapper.updateById(userDetail);
        //修改电卡
        int updateElectric = electricCardMapper.updateById(electricCard);

        //插入记录表
        RecodVo recodVo = detailPowerMapper.FindAddressById(detailPower1.getId());
        ExchangeRecord record=new ExchangeRecord();
        record.setAddress(recodVo.getAddress());
        record.setUid(user.getId());
        int insert = exchangeRecordMapper.insert(record);
        //修改预约表
        reservationRecord.setStatus(0);
        this.reservationRecordService.updateById(reservationRecord);

        if(updateUserDetail+i1+i2+updateElectric+insert>=5){
            return Result.success("换电成功！");
        }
        return Result.error("换电失败！");

    }
}




